//index.js
//获取应用实例
const app = getApp()
const getCurrentPagesApi = require('../../utils/common').getCurrentPagesApi;
var WxParse = require('../../wxParse/wxParse.js');
const banner = require('../../config').banner
const host = require('../../config').host
Page({
  data: {
    banners: [],
    shikuang: [],
    jiage: [],
    haiguan: [],
    currentTypeId: 1,
    currentHaiguanId: 0,
    currentNewsId: 1,
    jiageTabs:[],
    newsTabs:[{'id':1,title:'国内新闻'},
    					{'id':2,title:'国际新闻'}],
    typeList: [{'id':2,'tid':1,title:'实时价格'},
    					 {'id':3,'tid':2,title:'实时存库'},
    		   		 {'id':1,'tid':3,title:'市场解读'},],
    haiguanTabs: [],
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
    navs:[{img: "../../images/p1_3.png",name: "市场实况"},
          {img: "../../images/p1_4.png",name: "海关数据"},
          // {img: "../../images/p1_5.png",name: "订购数据"},
          // {img: "../../images/p1_6.png",name: "定制报告"},
          {img: "../../images/p1_7.png",name: "新闻资讯"},
          // {img: "../../images/p1_8.png",name: "板材查询"},
          {img: "../../images/p1_9.png",name: "English"},
          {img: "../../images/p2_1.png",name: "Русский"}]
  },
  catchtapCategory: function(e){
    var data = e.currentTarget.dataset
    app.globalData.currentCateType = {typeName: data.name}
    console.log(data.name);
    if(data.name=='市场实况'){
      wx.navigateTo({
            url: '../shikuang/shikuang'
          })
    }else if(data.name=='海关数据'){
      wx.navigateTo({
            url: '../haiguan/haiguan'
          })
    }else if(data.name=='新闻资讯'){
      wx.navigateTo({
            url: '../news/news'
          })
    }else if(data.name=='订购数据'){
      wx.navigateTo({
            url: './shuju'
          })
    }else if(data.name=='定制报告'){
      wx.navigateTo({
            url: './baogao'
          })
    }else if(data.name=='木材交易'){
      wx.navigateTo({
            url: '../jiaoyi/jiaoyi'
          })
    }else if(data.name=='English'){
      wx.navigateTo({
            url: '../english/english'
          })
    }else if(data.name=='Русский'){
      wx.navigateTo({
            url: '../russian/russian'
          })
    }
    
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  toBankuai: function (event) {
    console.log(event);
    var typeTid = event.currentTarget.dataset.typetid;
    var typeId = event.currentTarget.dataset.typeid;
    var functions = event.currentTarget.dataset.function;
    if(functions == "shikuangs"){
			this.shikuangs(typeId);
			this.setData({
	      currentTypeId:typeTid,
	    });
    }else if(functions == "jiages"){
    	this.jiages(typeId);
    	this.setData({
	      currentTypeId:typeId,
	    });
    }else if(functions == "haiguans"){
    	this.haiguans(typeId);
    	this.setData({
	      currentHaiguanId:typeId,
	    });
    }else if(functions == "newss"){
    	this.news(typeId);
    	this.setData({
	      currentNewsId:typeId,
	    });
    }
  },
  toNews: function () {
    wx.navigateTo({
    	url:'../news/news'
    })
  },toHaiguan: function () {
    wx.navigateTo({
    	url:'../haiguan/haiguan'
    })
  },
  toShikuang: function () {
    wx.navigateTo({
    	url:'../shikuang/shikuang'
    })
  },
  toJiage: function () {
    wx.navigateTo({
    	url:'../index/jiage'
    })
  },
  news: function(type) {
    var that = this
    wx.request({
      url: host+'api/index/news',
      data: {'type':type},
      success: function(res){
        that.setData({
          news: res.data
        })
      },
      fail: function(data){
        console.log(data)
      }
    })
  },
  shikuangs: function(type) {
    var that = this
    wx.request({
      url: host+'api/index/shikuang',
      data: {'type':type},
      success: function(res){
        that.setData({
          shikuang: res.data
        })
      },
      fail: function(data){
        console.log(data)
      }
    })
  },
  jiages: function(type) {
    var that = this
    wx.request({
      url: host+'api/index/jiage',
      data: {'id':type},
      success: function(res){
      	console.log(res.data);
        that.setData({
          jiage: res.data
        })
      },
      fail: function(data){
        console.log(data)
      }
    })
  },
  haiguans: function(type) {
    var that = this
    wx.request({
      url: host+'api/index/haiguan',
      data: {'id':type},
      success: function(res){
      	console.log(res.data);
        that.setData({
          haiguan: res.data
        })
      },
      fail: function(data){
        console.log(data)
      }
    })
  },
  shikuang_detail: function(e) {
    wx.navigateTo({
      url: `../shikuang/shikuang_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  haiguan_detail: function(e) {
    wx.navigateTo({
      url: `../haiguan/haiguan_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  news_detail: function(e) {
    wx.navigateTo({
      url: `../news/news_detail?id=${e.currentTarget.dataset.id}`
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return {
        title: '国数源',
        path: getCurrentPagesApi()
      }
    }
  },
  onLoad: function () {
  	// console.log('ddd');
      this.banner();
      this.shikuangs(2);
      this.haiguans(3);
      this.news(1);
      this.jiageTabss();
      this.haiguanTabss();
      this.jiages(1);
  },
  jiageTabss: function() {
    var that = this
    wx.request({
      url: host+'api/index/jiagetabs',
      success: function(res){
        that.setData({
          jiageTabs: res.data
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  haiguanTabss: function() {
    var that = this
    wx.request({
      url: host+'api/index/haiguantabs',
      success: function(res){
      	// console.log(res.data[0].id)
        that.setData({
          haiguanTabs: res.data,
          currentHaiguanId: res.data[0].id
        })
      },
      fail: function(data){
        // console.log(data)
      }
    })
  },
  banner: function() {
    var that = this
    wx.request({
      url: banner,
      success: function(res){
        that.setData({
          banners: res.data
        })
      }
    })
  },
  getUserInfo: function(e) {
    // console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
